<?php
/**
 * @title 2976. 转换字符串的最小成本 I
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $source
     * @param String $target
     * @param String[] $original
     * @param String[] $changed
     * @param Integer[] $cost
     * @return Integer
     */
    function minimumCost($source, $target, $original, $changed, $cost) {
        /**
         * @since 2024-01-15 字母转换的最小成本
         */
        $minCostArray = [];

        /**
         * @since 2024-01-15 遍历转换数组
         */
        $costCount = count($cost);
        for($i=0; $i<$costCount; $i++){
            /**
             * @since 2024-01-15 取最小值
             * 注意，可能存在下标 i 、j 使得 original[j] == original[i] 且 changed[j] == changed[i]
             */
            if(isset($minCostArray[$original[$i]][$changed[$i]])){
                $minCostArray[$original[$i]][$changed[$i]] = min($cost[$i], $minCostArray[$original[$i]][$changed[$i]]);
            } else {
                $minCostArray[$original[$i]][$changed[$i]] = $cost[$i];
            }
        }

        /**
         * @since 2024-01-15 所有的转换字符的起点
         */
        $charArray = array_keys($minCostArray);

        /**
         * @since 2024-01-15 遍历所有起点字符
         */
        while($charArray){
            /**
             * @since 2024-01-15 起点
             */
            $startChar = array_pop($charArray);

            /**
             * @since 2024-01-05 BFS广度遍历
             */
            $nodeArray = $minCostArray[$startChar];
            while($nodeArray){
                /**
                 * @since 2024-01-05 下次遍历元素的集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2024-01-05 遍历当前元素集合
                 */
                foreach ($nodeArray as $node=>$nodeCost){
                    // echo "node: ". implode(", ", array_keys($nodeArray)) ."\n";

                    /**
                     * @since 2024-01-15 node可以到达的点集合
                     */
                    if(isset($minCostArray[$node])){
                        // echo "node: {$node}, nextNode: ". implode(", ", array_keys($minCostArray[$node])) ."\n";

                        foreach ($minCostArray[$node] as $nextNode=>$nextCost){
                            /**
                             * @since 2024-01-15 不能回溯到起点
                             */
                            if($nextNode !== $startChar){
                                /**
                                 * @since 2024-01-15 startChar到nextNode的费用
                                 */
                                $totalCost = $nodeCost+$nextCost;
                                // echo "nextNode: {$nextNode}, cost: {$totalCost}\n";

                                /**
                                 * @since 2024-01-15 不存在，或者更低的费用
                                 */
                                if(!isset($minCostArray[$startChar][$nextNode]) OR $totalCost < $minCostArray[$startChar][$nextNode]){
                                    $minCostArray[$startChar][$nextNode] = $totalCost;
                                    $nextNodeArray[$nextNode] = $totalCost;
                                } else {}
                            } else {}
                        }
                    } else {}
                }

                /**
                 * @since 2024-01-05 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }
        }
        // return $minCostArray;

        /**
         * @since 2024-01-15 总费用
         */
        $totalCost = 0;

        /**
         * @since 2024-01-15 字符串转换
         */
        for($i=0; $i<strlen($source); $i++){
            /**
             * @since 2024-01-15 字符不相同
             */
            if($source[$i] != $target[$i]){
                /**
                 * @since 2024-01-15 是否存在转换费用
                 */
                if(isset($minCostArray[$source[$i]][$target[$i]])){
                    $totalCost += $minCostArray[$source[$i]][$target[$i]];
                } else {
                    /**
                     * @since 2024-01-15 不存在转换费用
                     * @return Integer
                     */
                    return -1;
                }
            } else {}
        }

        /**
         * @return Integer
         */
        return $totalCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.15_3.php
 */
$datas = [
    // ["abcd", "acbe", ["a","b","c","c","e","d"], ["b","c","b","e","b","e"], [2,5,5,1,2,20]], // 28
    // ["aaaa", "bbbb", ["a","c"], ["c","b"], [1,2]], // 12
    // ["abcd", "abce", ["a"], ["e"], [10000]], // -1
    // ["bcaabaddac", "bdccbdaadc", ["c","d","a","a","c","a","d"], ["a","a","d","b","d","c","c"], [4,3,6,3,11,6,4]], // 40
    ["aaadbdcdac", "cdbabaddba", ["a","c","b","d","b","a","c"], ["c","a","d","b","c","b","d"], [7,2,1,3,6,1,7]], // 39
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/2976-531.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->minimumCost(...$data);
    // echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
